package com.dhcc.android.base.utils.dataparse;

import android.util.Xml;

import com.dhcc.android.base.common.bean.Action;
import com.dhcc.android.base.common.bean.Check;
import com.dhcc.android.base.common.bean.Item;
import com.dhcc.android.base.common.bean.Node;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zhoudan
 * @date 2015年5月25日
 */
public class NodeParse {

    /**
     * 解析层级目录选择配置文件 DEMAND/c_task.xml
     */
    public static List<Node> parseNodeList(InputStream is) {
        List<Node> list = null;
        Node node = null;
        Check check = null;
        Item item = null;
        List<Action> actions = null;
        Action action = null;

        PullParse pullParse = new PullParse();
        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(is, "UTF-8");
            // 事件类型
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        list = new ArrayList<Node>();

                        break;
                    case XmlPullParser.START_TAG:
                        // 获得当前节点元素的名称
                        String name = parser.getName();

                        if ("NODE".equalsIgnoreCase(name)) {
                            node = new Node();
                            pullParse.setChildParam(parser, node);
                        } else if ("CHECK".equalsIgnoreCase(name)) {
                            check = new Check();
                            pullParse.setChildParam(parser, check);
                            node.setCheck(check);

                            check = null;

                        } else if ("ITEM".equalsIgnoreCase(name)) {
                            item = new Item();
                            pullParse.setChildParam(parser, item);
                            node.setItem(item);

                            item = null;
                        } else if ("ACTIONS".equalsIgnoreCase(name)) {
                            actions = new ArrayList<Action>();

                        } else if ("ACTION".equalsIgnoreCase(name)) {
                            action = new Action();
                            pullParse.setChildParam(parser, action);
                            actions.add(action);

                            action = null;
                        }

                        break;
                    case XmlPullParser.END_TAG:
                        name = parser.getName();

                        if ("NODE".equalsIgnoreCase(name)) {
                            list.add(node);
                            node = null;
                        } else if ("ACTIONS".equalsIgnoreCase(name)) {
                            node.setActions(actions);

                            actions = null;
                        }
                        break;

                    default:
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}
